﻿using apointmentbase.core;
using apointmentbase.core.Login;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace apointment.LibaryApointment
{
    /// <summary>
    /// 预约
    /// </summary>
    public class PersonApointMent
    {
        public static async Task<string> Login(LoginHeader login,Dictionary<string,string>dic)
        {
            string result = "";

            var req = (HttpWebRequest)WebRequest.Create(login.url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Headers.Add("session-id", "supxn5dgbpelxba3222syiyw");
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat";

            #region 添加Post 参数
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            var resp = await req.GetResponseAsync();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            var serializerJson = JsonConvert.DeserializeObject<LoginResultRoot>(result);
           //返回状态码
            return serializerJson.res;
        }

    }
}
